package com.wmh.baseservice.robot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.common.utils.CollectionUtil;
import com.wmh.baseservice.robot.entity.RobotTask;
import com.wmh.baseservice.robot.mapper.RobotTaskMapper;
import com.wmh.baseservice.robot.pojo.vo.RobotTaskUpdateVO;
import com.wmh.baseservice.robot.service.RobotTaskService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 20402
 * @description 针对表【robot_task】的数据库操作Service实现
 * @createDate 2022-10-29 17:10:52
 */
@Service
public class RobotTaskServiceImpl extends ServiceImpl<RobotTaskMapper, RobotTask>
        implements RobotTaskService {


    private void checkIsExist(Long id) {
        RobotTask entity = baseMapper.selectById(id);
        if (entity == null) {
            throw new ServiceException("该数据不存在!");
        }
    }

    @Override
    public void commitRobotTask(String name, String viewRouter) {
        RobotTask entity = baseMapper.selectOne(new LambdaQueryWrapperX<RobotTask>()
                .eq(RobotTask::getName, name)
                .eq(RobotTask::getViewRouter, viewRouter));

        // 更新时间
        if (!Objects.isNull(entity)) {
            entity.setUpdateTime(null);
            // 新增
        } else {
            entity = new RobotTask();
            entity.setName(name)
                     .setViewRouter(viewRouter)
                     .setEnable(1);
        }
        saveOrUpdate(entity);
    }

    @Override
    public void deleteRobotTaskById(Long id) {
        checkIsExist(id);
        baseMapper.delete(new LambdaQueryWrapper<RobotTask>().eq(RobotTask::getId, id));
    }

    @Override
    public void updateRobotTaskById(RobotTaskUpdateVO reqVO) {
        // 检查是否存在要修改的任务
        checkIsExist(reqVO.getId());

        RobotTask rt = new RobotTask();
        rt.setId(reqVO.getId())
                .setName(reqVO.getName())
                .setViewRouter(reqVO.getViewRouter());

        UpdateWrapper<RobotTask> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", reqVO.getId());
        baseMapper.update(rt, updateWrapper);

    }

    @Override
    public void modifyStatusById(Long id) {
        // 检查是否存在要修改的任务
        checkIsExist(id);

        // 构建更新条件
        LambdaUpdateWrapper<RobotTask> updateWrapper = new LambdaUpdateWrapper<RobotTask>()
                .eq(RobotTask::getId, id);

        // 查询当前状态
        RobotTask entity = baseMapper.selectById(id);
        int currentStatus = entity.getEnable();

        // 计算新状态（0 切换为 1，1 切换为 0）
        int newStatus = currentStatus == 0 ? 1 : 0;

        // 执行更新
        baseMapper.update(null, updateWrapper.set(RobotTask::getEnable, newStatus));
    }

    @Override
    public List<RobotTask> getByIds(List<Long> ids) {
        return baseMapper.selectList(new LambdaQueryWrapperX<RobotTask>()
                .in(RobotTask::getId,ids));
    }

    @Override
    public Map<Long, String> getIdNameMap(List<Long> taskIds) {
        return CollectionUtil.convertMap(getByIds(taskIds),RobotTask::getId,RobotTask::getName);
    }
}

